package com.tankwar.view.page;

import com.swak.fx.support.Dialogs;
import com.swak.fx.support.Display;
import com.swak.ui.Event;
import com.swak.ui.EventListener;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.Window;
import com.tankwar.view.panel.ScenePanel;

import javafx.fxml.FXML;
import javafx.scene.control.ButtonType;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * 界面
 * 
 * @author lifeng
 */
@FXMLView(title = "SWAK.坦克大战", value = "MainPage.fxml", css = "MainPage.css", stageStyle = "TRANSPARENT")
public class MainPage extends Window implements EventListener {

	private Stage settings;
	private ScenePanel scenePanel;

	@FXML
	public void initialize() {
		double h = Display.getScreen().getVisualBounds().getHeight() * 0.93;
		double w = h * 1.66;
		this.root.setPrefWidth(w);
		this.root.setPrefHeight(h);
		this.scenePanel = new ScenePanel();
		((BorderPane) this.root).setCenter(scenePanel);
		Display.getEventBus().register(this);
		super.initialize();
	}

	/**
	 * 关闭
	 */
	@FXML
	public void onClose(MouseEvent evt) {
		Display.runUI(() -> {
			ButtonType res = Dialogs.confirm("提醒", "确认关闭？", ButtonType.YES, ButtonType.NO);
			if (res == ButtonType.YES) {
				this.onHide(evt);
				if (settings != null) {
					settings.close();
				}
				Display.getEventBus().post(Event.EXIT);
			}
		});
	}

	/**
	 * 页面显示后
	 */
	@Override
	public void onReady() {
		this.scenePanel.onReady();
	}

	/**
	 * 监听事件
	 */
	@Override
	public void listen(Event event) {
		if (event.is(Event.CLOSE)) {
			this.onClose(null);
		}
	}
}